package com.yjw.yunmusic.web.control;

import com.yjw.yunmusic.web.model.Comment;
import com.yjw.yunmusic.web.model.SongInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SongInfoControl {

    private static Map<String,SongInfo> songInfoMap = new HashMap<>();

    @Autowired
    private CommentPostControl commentPostControl;

    @RequestMapping(value = "/songinfo")
    public String index(String songId, ModelMap modelMap){

        if(songId == null){
            songId="35847388";
        }
        else if(!songInfoMap.containsKey(songId)){
            return "goodbye";
        }
        SongInfo songInfo=songInfoMap.get(songId);
        modelMap.addAttribute("song",songInfo);

        List<Comment> comments = commentPostControl.getComments(songId);
        modelMap.addAttribute("comments",comments);

        return "index";
    }

    @RequestMapping(value = "/songinfo/get")
    @ResponseBody
    public SongInfo get(String songId){
        SongInfo songInfo=songInfoMap.get(songId);
        return songInfo;
    }

    @PostConstruct
    public void init(){
        //Hello
        SongInfo songInfo= new SongInfo();
        songInfo.setId("35847388");
        songInfo.setName("Hello");
        songInfo.setSinger("Adele");
        songInfo.setAlbumName("Hello");
        songInfo.setCommentCount(10);
        songInfo.setAlbumImg("http://p2.music.126.net/PrO7oPvnjvH4xdujdGbf2w==/1407374890649284.jpg?param=130y130");
        songInfo.setLyrics("Hello, it's me<br/> 你好吗 是我<br/>" +
                "I was wondering if after all these years you'd like to meet");
        songInfoMap.put("35847388",songInfo);

        //执此一念
        songInfo= new SongInfo();
        songInfo.setId("1375466014");
        songInfo.setName("执此一念");
        songInfo.setSinger("李上安");
        songInfo.setAlbumName("执此一念");
        songInfo.setCommentCount(20);
        songInfo.setAlbumImg("http://p1.music.126.net/EkUfUje0oUZyRVYRrC7Psw==/109951164188360879.jpg?param=130y130");
        songInfo.setLyrics("仍然是安静夜里三点睡我<br/> 躺下来闭上眼非常疲惫<br/>远处的霓虹闪得好刺眼<br/>");
        songInfoMap.put("1375466014",songInfo);

        //像鱼
        songInfo= new SongInfo();
        songInfo.setId("1331819951");
        songInfo.setName("像鱼");
        songInfo.setSinger("王贰浪");
        songInfo.setAlbumName("像鱼");
        songInfo.setCommentCount(30);
        songInfo.setAlbumImg("http://p2.music.126.net/PrO7oPvnjvH4xdujdGbf2w==/1407374890649284.jpg?param=130y130");
        songInfo.setLyrics("这是一首简单的歌<br/>" +
                "没有什么独特<br/>"+"试着代入我的心事<br/>");
        songInfoMap.put("1331819951",songInfo);
    }
}
